package com.amt.iptvsetting;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;

import java.io.File;
import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class VideoRecordActivity extends AppCompatActivity implements SurfaceHolder.Callback {

    @BindView(R.id.activity_video_record_sv)
    SurfaceView activityVideoRecordSv;
    @BindView(R.id.activity_video_record_tv)
    TextView activityVideoRecordTv;

    private SurfaceHolder surfaceHolder;
    private MediaRecorder mediarecorder;// 录制视频的类
    private boolean isStart = false;

    public static void instance(Activity context) {

        Intent intent = new Intent();
        intent.setClass(context, VideoRecordActivity.class);
        context.startActivity(intent);

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_record);
        ButterKnife.bind(this);

        SurfaceHolder holder = activityVideoRecordSv.getHolder();// 取得holder
        holder.addCallback(this); // holder加入回调接口
        // setType必须设置，要不出错.
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    private void initMediaRecord() {

        mediarecorder = new MediaRecorder();// 创建mediarecorder对象

        // 设置音频采集方式
        mediarecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        //设置视频的采集方式
        mediarecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        // 设置录制完成后视频的封装格式THREE_GPP为3gp.MPEG_4为mp4
        mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        // 设置录制的视频编码h263 h264
        mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        // 设置视频录制的分辨率。必须放在设置编码和格式的后面，否则报错
        mediarecorder.setVideoSize(176, 144);
        // 设置录制的视频帧率。必须放在设置编码和格式的后面，否则报错
        mediarecorder.setVideoFrameRate(30);

        mediarecorder.setPreviewDisplay(surfaceHolder.getSurface());
        // 设置视频文件输出的路径
        mediarecorder.setOutputFile(Environment.getExternalStorageDirectory()+ File.separator + "zhouwd" + File.separator + System.currentTimeMillis() +  ".mp4");
        try {
            mediarecorder.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    @OnClick(R.id.activity_video_record_tv)
    public void onActivityVideoRecordTvClicked() {

        if (isStart) {
            isStart = false;
            activityVideoRecordTv.setText("开始录制");

            mediarecorder.stop();
            mediarecorder.release();
        } else {
            isStart = true;
            activityVideoRecordTv.setText("暂停录制");
            initMediaRecord();
            mediarecorder.start();
        }

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // 将holder，这个holder为开始在oncreat里面取得的holder，将它赋给surfaceHolder
        surfaceHolder = holder;
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // 将holder，这个holder为开始在oncreat里面取得的holder，将它赋给surfaceHolder
        surfaceHolder = holder;
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // surfaceDestroyed的时候同时对象设置为null
        activityVideoRecordSv = null;
        surfaceHolder = null;
        mediarecorder = null;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mediarecorder.release();
    }
}
